﻿namespace KeRui.Common
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    public class UBB
    {
        private static RegexOptions options = RegexOptions.IgnoreCase;

        public static string ClearBR(string sDetail)
        {
            return Regex.Replace(sDetail, @"[\r\n]", string.Empty, RegexOptions.IgnoreCase);
        }

        public static string ClearUBB(string sDetail)
        {
            return Regex.Replace(sDetail, @"\[[^\]]*?\]", string.Empty, RegexOptions.IgnoreCase);
        }

        private static string Parsecode(string text, string prefix, ref int pcodecount, ref StringBuilder builder)
        {
            text = Regex.Replace(text, @"^[\n\r]*([\s\S]+?)[\n\r]*$", "$1", options);
            if (!builder.ToString().Equals(""))
            {
                builder.Append("<>");
            }
            builder.Append("<br /><br /><div class=\"msgheader\"><div class=\"right\"><a href=\"###\" class=\"smalltxt\" onclick=\"copycode($('code" + prefix + "_" + ((int) pcodecount).ToString() + "'));\">[复制到剪贴板]</a></div>CODE:</div><div id=\"code" + prefix + "_" + ((int) pcodecount).ToString() + "\" class=\"msgborder\">");
            builder.Append(text);
            builder.Append("</div><br /><br />");
            pcodecount++;
            text = "[\tDISCUZ_CODE_" + prefix + "_" + ((int) pcodecount).ToString() + "\t]";
            return text;
        }

        private static string ParseTable(string str)
        {
            string newValue = "";
            string str3 = "";
            string str4 = "";
            int index = str.ToLower().IndexOf("[table");
            Regex regex = new Regex(@"\s*\[table(=(\d{1,4}%?)(,([\(\)%,#\w]+))?)?\][\n\r]*([\s\S]+?)[\n\r]*\[\/table\]\s*", options);
            while ((index >= 0) && (str.ToLower().IndexOf("[/table]") >= 0))
            {
                for (Match match = regex.Match(str); match.Success; match = match.NextMatch())
                {
                    str3 = match.Groups[2].ToString();
                    str3 = Utils.CutString(str3, str3.Length - 1, str3.Length).Equals("%") ? ((Utils.StrToInt(Utils.CutString(str3, 0, str3.Length - 1), 100) <= 0x62) ? str3 : "98%") : ((Utils.StrToInt(str3, 560) <= 560) ? str3 : "560");
                    str4 = match.Groups[4].ToString();
                    newValue = "<table class=\"t_table\" cellspacing=\"1\" cellpadding=\"4\" ";
                    newValue = (newValue + (str3.Equals("") ? "" : ("width=\"" + str3 + "\" "))) + ("".Equals(str4) ? "" : ("style=\"background: " + str4 + "; \" ")) + ">";
                    str3 = Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(match.Groups[5].ToString(), @"\[td=(\d{1,2}),(\d{1,2})(,(\d{1,4}%?))?\]", "<td colspan=\"$1\" rowspan=\"$2\" width=\"$4\" class=\"t_table\">", options), @"\[tr\]", "<tr>", options), @"\[td\]", "<td>", options), @"\[\/td\]", "</td>", options), @"\[\/tr\]", "</tr>", options), @"\<td\>\<\/td\>", "<td>&nbsp;</td>", options);
                    newValue = newValue + str3 + "</table>";
                    str = str.Replace(match.Groups[0].ToString(), newValue);
                }
                index = str.ToLower().IndexOf("[table", (int) (index + 7));
            }
            return str;
        }

        public static string UBBToHTML(string sDetail)
        {
            Match match;
            StringBuilder builder = new StringBuilder();
            int pcodecount = -1;
            string newValue = "";
            string prefix = Environment.TickCount.ToString();
           
            Regex regex = new Regex(@"\s*\[code\]([\s\S]+?)\[\/code\]\s*", options);
            for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
            {
                newValue = Parsecode(match.Groups[1].ToString(), prefix, ref pcodecount, ref builder);
                sDetail = sDetail.Replace(match.Groups[0].ToString(), newValue);
            }
            sDetail = Regex.Replace(sDetail, @"\[smilie\](.+?)\[\/smilie\]", "<img src=\"$1\" />", options);
          
            sDetail = Regex.Replace(sDetail, @"\[b(?:\s*)\]([\s]||[\s\S]+?)\[/b(?:\s*)\]", "<b>$1</b>", options);
            sDetail = Regex.Replace(sDetail, @"\[i(?:\s*)\]([\s]||[\s\S]+?)\[/i(?:\s*)\]", "<i>$1</i>", options);
            sDetail = Regex.Replace(sDetail, @"\[u(?:\s*)\]([\s]||[\s\S]+?)\[/u(?:\s*)\]", "<u>$1</u>", options);
            sDetail = Regex.Replace(sDetail, @"((\r\n)*\[p\])(.*?)((\r\n)*\[\/p\])", "<p>$3</p>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            sDetail = Regex.Replace(sDetail, @"\[sup(?:\s*)\]([\s]||[\s\S]+?)\[/sup(?:\s*)\]", "<sup>$1</sup>", options);
            sDetail = Regex.Replace(sDetail, @"\[sub(?:\s*)\]([\s]||[\s\S]+?)\[/sub(?:\s*)\]", "<sub>$1</sub>", options);
            sDetail = Regex.Replace(sDetail, @"\[url(?:\s*)\](www\.(.*?))\[/url(?:\s*)\]", "<a href=\"http://$1\" target=\"_blank\">$1</a>", options);
            sDetail = Regex.Replace(sDetail, "\\[url(?:\\s*)\\]\\s*((https?://|ftp://|gopher://|news://|telnet://|rtsp://|mms://|callto://|bctp://|ed2k://)([^\\[\"']+?))\\s*\\[\\/url(?:\\s*)\\]", "<a href=\"$1\" target=\"_blank\">$1</a>", options);
            sDetail = Regex.Replace(sDetail, "\\[url=www.([^\\[\"']+?)(?:\\s*)\\]([\\s\\S]+?)\\[/url(?:\\s*)\\]", "<a href=\"http://www.$1\" target=\"_blank\">$2</a>", options);
            sDetail = Regex.Replace(sDetail, "\\[url=((https?://|ftp://|gopher://|news://|telnet://|rtsp://|mms://|callto://|bctp://|ed2k://)([^\\[\"']+?))(?:\\s*)\\]([\\s\\S]+?)\\[/url(?:\\s*)\\]", "<a href=\"$1\" target=\"_blank\">$4</a>", options);
            sDetail = Regex.Replace(sDetail, @"\[email(?:\s*)\](.*?)\[\/email\]", "<a href=\"mailto:$1\" target=\"_blank\">$1</a>", options);
            sDetail = Regex.Replace(sDetail, @"\[email=(.[^\[]*)(?:\s*)\](.*?)\[\/email(?:\s*)\]", "<a href=\"mailto:$1\" target=\"_blank\">$2</a>", options);
            sDetail = Regex.Replace(sDetail, @"\[color=([^\[\<]+?)\]([\s]||[\s\S]+?)\[/color(?:\s*)\]", "<font color=\"$1\">$2</font>", options);
            sDetail = Regex.Replace(sDetail, @"\[size=(\d+?)\]([\s]||[\s\S]+?)\[/size(?:\s*)\]", "<font size=\"$1\">$2</font>", options);
            sDetail = Regex.Replace(sDetail, @"\[size=(\d+(\.\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\]([\s]||[\s\S]+?)\[/size(?:\s*)\]", "<font style=\"font-size: $1\">$2</font>", options);
            sDetail = Regex.Replace(sDetail, @"\[font=([^\[\<]+?)\]([\s]||[\s\S]+?)\[/font(?:\s*)\]", "<font face=\"$1\">$2</font>", options);
            sDetail = Regex.Replace(sDetail, @"\[align=([^\[\<]+?)\]([\s]||[\s\S]+?)\[/align(?:\s*)\]", "<p align=\"$1\">$2</p>", options);
            sDetail = Regex.Replace(sDetail, @"\[float=([^\[\<]+?)\]([\s]||[\s\S]+?)\[/float(?:\s*)\]", "<br style=\"clear: both\" /><span style=\"float: $1;\">$2</span>", options);
            sDetail = Regex.Replace(sDetail, @"\[indent(?:\s*)\]([\s]||[\s\S]+?)\[/indent(?:\s*)\]", "<blockquote>$1</blockquote>", options);
            sDetail = Regex.Replace(sDetail, @"\[simpletag(?:\s*)\]([\s]||[\s\S]+?)\[/simpletag(?:\s*)\]", "<blockquote>$1</blockquote>", options);
            sDetail = Regex.Replace(sDetail, @"\[list\]([\s]||[\s\S]+?)\[/list\]", "<ul>$1</ul>", options);
            sDetail = Regex.Replace(sDetail, @"\[list=(1|A|a|I|i| )\]([\s]||[\s\S]+?)\[/list\]", "<ul type=\"$1\">$2</ul>", options);
            sDetail = Regex.Replace(sDetail, @"\[\*\]", "<li>", options);
            sDetail = ParseTable(sDetail);
            sDetail = Regex.Replace(sDetail, @"(\[SHADOW=)(\d*?),(#*\w*?),(\d*?)\]([\s]||[\s\S]+?)(\[\/SHADOW\])", "<table width='$2'  style='filter:SHADOW(COLOR=$3, STRENGTH=$4)'>$5</table>", options);
            sDetail = Regex.Replace(sDetail, @"(\[glow=)(\d*?),(#*\w*?),(\d*?)\]([\s]||[\s\S]+?)(\[\/glow\])", "<table width='$2'  style='filter:GLOW(COLOR=$3, STRENGTH=$4)'>$5</table>", options);
            sDetail = Regex.Replace(sDetail, @"\[center\]([\s]||[\s\S]+?)\[\/center\]", "<center>$1</center>", options);
            int index = sDetail.ToLower().IndexOf("[quote]");
            int num3 = 0;
            while (((index >= 0) && (sDetail.ToLower().IndexOf("[/quote]") >= 0)) && (num3 < 3))
            {
                num3++;
                sDetail = Regex.Replace(sDetail, @"\[quote\]([\s\S]+?)\[/quote\]", "<br /><br /><div class=\"msgheader\">引用:</div><div class=\"msgborder\">$1</div>", options);
                index = sDetail.ToLower().IndexOf("[quote]", (int) (index + 7));
            }
          
            sDetail = sDetail.Replace("&amp;", "&");
            sDetail = Regex.Replace(sDetail, @"^((ed2k|thunder|vagaa):\/\/[\[\]\|A-Za-z0-9\.\/=\?%\-&_~`@':+!]+)", "<a target=\"_blank\" href=\"$1\">$1</a>", options);
            sDetail = Regex.Replace(sDetail, @"((ed2k|thunder|vagaa):\/\/[\[\]\|A-Za-z0-9\.\/=\?%\-&_~`@':+!]+)$", "<a target=\"_blank\" href=\"$1\">$1</a>", options);
            sDetail = Regex.Replace(sDetail, "[^>=\\]\"]((ed2k|thunder|vagaa):\\/\\/[\\[\\]\\|A-Za-z0-9\\.\\/=\\?%\\-&_~`@':+!]+)", "<a target=\"_blank\" href=\"$1\">$1</a>", options);
          
            sDetail = Regex.Replace(sDetail, @"\[img\]\s*(http://[^\[\<\r\n]+?)\s*\[\/img\]", "<img src=\"$1\" border=\"0\" />", options);
            sDetail = Regex.Replace(sDetail, @"\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*(http://[^\[\<\r\n]+?)\s*\[\/img\]", "<img src=\"$3\" width=\"$1\" height=\"$2\" border=\"0\" />", options);
            sDetail = Regex.Replace(sDetail, @"\[image\](http://[\s\S]+?)\[/image\]", "<img src=\"$1\" border=\"0\" />", options);
            pcodecount = 0;
            foreach (string str3 in Utils.SplitString(builder.ToString(), "<>"))
            {
                sDetail = sDetail.Replace("[\tDISCUZ_CODE_" + prefix + "_" + pcodecount.ToString() + "\t]", str3);
                pcodecount++;
            }
            sDetail = sDetail.Replace("\t", "&nbsp; &nbsp; ");
            sDetail = sDetail.Replace("  ", "&nbsp; ");
            sDetail = Regex.Replace(sDetail, @"\[r/\]", "\r", options);
            sDetail = Regex.Replace(sDetail, @"\[n/\]", "\n", options);
            regex = new Regex(@"(\r\n((&nbsp;)|　)+)(?<正文>\S+)", options);
            for (match = regex.Match(sDetail); match.Success; match = match.NextMatch())
            {
                sDetail = sDetail.Replace(match.Groups[0].ToString(), "<br />　　" + match.Groups["正文"].ToString());
            }
            sDetail = sDetail.Replace("\r\n", "<br />");
            sDetail = sDetail.Replace("\r", "");
            sDetail = sDetail.Replace("\n\n", "<br /><br />");
            sDetail = sDetail.Replace("\n", "<br />");
            sDetail = sDetail.Replace("{rn}", "\r\n");
            sDetail = sDetail.Replace("{nn}", "\n\n");
            sDetail = sDetail.Replace("{r}", "\r");
            sDetail = sDetail.Replace("{n}", "\n");
            return sDetail;
        }
    }
}

